package com.crrc.dataparse.parse.PostAlg;

import com.crrc.dataparse.entity.*;
import com.crrc.dataparse.parse.IPostAlgo;
import com.crrc.dataparse.util.ByteConvertUtil;

import java.util.Date;

/*
    协议处理方案
    牵引多频率参数包使用
 */
public class MFPost implements IPostAlgo {
    //各类型参数的代号
    private static final int BASE_INFO_PARAMATER_Line = 7;
    private static final int BASE_INFO_PARAMATER_TrainType = 11;
    private static final int BASE_INFO_PARAMATER_Car = 8;
    private static final int BASE_INFO_PARAMATER_Terminal = 9;
    private static final int BASE_INFO_PARAMATER_Time = 10;
    /**
     *
     * @param packetData: 原始数据包
     * @param protocol: 协议信息
     * @param pdp: 解析后数据包
     * @param paserInfo: 处理方案信息
     * @return 包头解析完毕后原始数据的offset（下一项解析从该offset开始）
     * @author MengXiangTong
     * @date 2021/11/9 15:54
     */
    @Override
    public int parseProtocolHead(String packetData, Protocol protocol, ParsedResult pdp, PaserInfo paserInfo) throws Exception {
        int offset = 0;

        byte[] data = ByteConvertUtil.binaryStringToBytes(packetData);

        for (Pro2TemPara p : protocol.getParaList()) {
            TemplatePara para = paserInfo.getIdParaMap().get(p.getParaOid());
            Integer paraType = para.getParameterType();
            if(paraType == BASE_INFO_PARAMATER_Line){
                pdp.setlineNo(para.getParameterID());
                pdp.addBaseInfo(para.getParameterID(), packetData.substring(offset, offset + para.getLength()));
            }
            else if(paraType == BASE_INFO_PARAMATER_Car){
                pdp.setmetroNo(para.getParameterID());
                pdp.addBaseInfo(para.getParameterID(), packetData.substring(offset, offset + para.getLength()));
            }
            else if(paraType == BASE_INFO_PARAMATER_Terminal){
                pdp.setTerminal(para.getParameterID());
                pdp.addBaseInfo(para.getParameterID(), packetData.substring(offset, offset + para.getLength()));
            }
            else if(paraType == BASE_INFO_PARAMATER_Time){
                pdp.addBaseInfo(para.getParameterID(), packetData.substring(offset, offset + para.getLength()));
            }
            else if(paraType == BASE_INFO_PARAMATER_TrainType){
                pdp.setTrainType(para.getParameterID());
                pdp.addBaseInfo(para.getParameterID(), packetData.substring(offset, offset + para.getLength()));
            }
            offset += para.getLength();
        }
        return offset;
    }
    /**
     *
     * @param packetData:原始数据包
     * @param paserInfo: 处理方案信息
     * @param protocol: 协议信息
     * @return 返回模板
     * @author MengXiangTong
     * @date 2021/11/9 15:56
     */
    @Override
    public Template getTemplate(String packetData, PaserInfo paserInfo, Protocol protocol) throws Exception {
        return protocol.getPro2TempList().get(0).getTemplate();
    }
    /**
     *
     * @param pdp: 解析后数据包
     * @param timeaccuracy: 时间精度
     * @return 设置各种字段后的解析后数据包
     * @author XinXin
     * @date 2025/8/15 16:07
     */
    public ParsedResult postProcess_mf(ParsedResult pdp){
        pdp.setlineNo(pdp.getBaseInfoMap().get(pdp.getlineNo()));
        pdp.setmetroNo(pdp.getBaseInfoMap().get(pdp.getmetroNo()));
        pdp.setTerminal(pdp.getBaseInfoMap().get(pdp.getTerminal()));
        pdp.setTrainType(pdp.getBaseInfoMap().get(pdp.getTrainType()));
        long timestamp = Long.valueOf(pdp.getBaseInfoMap().get("Time"));
        pdp.setTimestamp(timestamp);
        return pdp;
    }

    public ParsedResult postProcess_mp(ParsedResult pdp,int increateTime) throws Exception {
        long timestamp = Long.valueOf(pdp.getBaseInfoMap().get("Time"));
        pdp.setTimestamp(timestamp+increateTime);
        return pdp;
    }
}
